package com.azkj.album.presenter;

import com.azkj.album.dto.LoginBean;
import com.azkj.album.dto.WxBean;
import com.azkj.album.network.Constants;
import com.azkj.album.network.NetworkMaster;
import com.azkj.album.network.callback.ServiceCallback;
import com.azkj.album.network.networkframe.bean.BaseResp;
import com.azkj.album.network.networkframe.net.HttpsUtil;
import com.azkj.album.network.networkframe.net.exception.ApiException;
import com.azkj.album.network.utils.StoreUtils;
import com.azkj.album.view.base.BasePresenter;
import com.azkj.album.view.iview.ILoginView;
import com.azkj.album.view.widgets.dialog.DialogHelper;

import java.util.Map;

public class LoginPresenter extends BasePresenter {

    private final ILoginView iLoginView;
    private String unionid;
    private String wx_username;


    public LoginPresenter(ILoginView iLoginView) {
        this.iLoginView = iLoginView;

    }

    public void login(Map<String, Object> body) {
        DialogHelper.showLoadingDialog("登录中", 0);
        NetworkMaster.getInstance().getCommonService().login(body, new ServiceCallback<BaseResp<LoginBean>>() {
            @Override
            public void onSuccess(BaseResp<LoginBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iLoginView.loginSuccess(false, jsonObj.getData());
                } else {
                    iLoginView.loginFail(jsonObj.getMsg());
                }

            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iLoginView.loginFail(apiException.errorInfo.error);
            }

        });

    }

    public void loginSms(Map<String, Object> body) {
        DialogHelper.showLoadingDialog("登录中", 0);
        NetworkMaster.getInstance().getCommonService().loginSms(body, new ServiceCallback<BaseResp<LoginBean>>() {
            @Override
            public void onSuccess(BaseResp<LoginBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iLoginView.loginSuccess(false, jsonObj.getData());
                } else {
                    iLoginView.loginFail(jsonObj.getMsg());
                }

            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iLoginView.loginFail(apiException.errorInfo.error);
            }

        });

    }

    public void loginVisitor(Map<String, Object> body) {
        DialogHelper.showLoadingDialog("登录中", 0);
        NetworkMaster.getInstance().getCommonService().loginVisitor(body, new ServiceCallback<BaseResp<LoginBean>>() {
            @Override
            public void onSuccess(BaseResp<LoginBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    iLoginView.loginSuccess(true, jsonObj.getData());
                } else {
                    iLoginView.loginFail(jsonObj.getMsg());
                }

            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iLoginView.loginFail(apiException.errorInfo.error);
            }

        });
    }

    public void getWxOpenID(String code) {
        DialogHelper.showLoadingDialog("登录中", 0);

        NetworkMaster.getInstance().getCommonService().getWxOpenID(Constants.WX_APP_ID, Constants.WX_APP_SECRET, code,
                "authorization_code", new ServiceCallback<WxBean>() {
                    @Override
                    public void onSuccess(WxBean jsonObj) {
                        DialogHelper.hideDialog();
                        if (jsonObj.getErrcode() == 0) {
                            getWxUserInfo(jsonObj.getAccess_token(), jsonObj.getOpenid());
                        } else {
                            iLoginView.loginFail(jsonObj.getErrmsg());
                            DialogHelper.hideDialog();
                        }

                    }

                    @Override
                    public void onError(ApiException apiException) {
                        DialogHelper.hideDialog();
                        iLoginView.loginFail(apiException.errorInfo.error);
                    }

                });
    }

    public void getWxUserInfo(String access_token, String openId) {

        NetworkMaster.getInstance().getCommonService().getWxUserInfo(access_token, openId, new ServiceCallback<WxBean>() {
            @Override
            public void onSuccess(WxBean jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getErrcode() == 0) {
                    unionid = jsonObj.getUnionid();
                    wx_username = jsonObj.getNickname();
                    loginWx(jsonObj.getUnionid());
                } else {
                    iLoginView.loginFail(jsonObj.getErrmsg());
                    DialogHelper.hideDialog();
                }

            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iLoginView.loginFail(apiException.errorInfo.error);
            }

        });
    }

    public void loginWx(String code) {
        Map<String, Object> body = HttpsUtil.getCommonPostRequest();
        body.put("wx_unionid", code);

        NetworkMaster.getInstance().getCommonService().loginWx(body, new ServiceCallback<BaseResp<LoginBean>>() {
            @Override
            public void onSuccess(BaseResp<LoginBean> jsonObj) {
                DialogHelper.hideDialog();
                if (jsonObj.getCode() == 1) {
                    StoreUtils.put("isWX", true);
                    iLoginView.loginSuccess(false, jsonObj.getData());

                } else {
                    iLoginView.loginFail(jsonObj.getMsg());
                }
            }

            @Override
            public void onError(ApiException apiException) {
                DialogHelper.hideDialog();
                iLoginView.loginFail(apiException.errorInfo.error);
            }

        });
    }

    public String getUnionid() {
        return unionid;
    }

    public String getWx_username() {
        return wx_username;
    }
}
